package org.example.leetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
public class HasCycle {
    class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }
        ListNode nodeOne = head;
        ListNode nodeTwo = head;
        while (nodeOne != null && nodeTwo != null && nodeOne.next != null && nodeTwo.next.next != null) {

            nodeOne = nodeOne.next;
            nodeTwo = nodeTwo.next.next;
            if (nodeOne.equals(nodeTwo)) {
                return true;
            }
        }
        return false;
    }
}
